package server;

import client.PaintAction;
import client.PaintingModel;

import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * Created by IntelliJ IDEA.
 * User: Hp
 * Date: 23.04.11
 * Time: 21:18
 * To change this template use File | Settings | File Templates.
 */
public class ServerModel {
    PaintingModel pModel;
    private int count;
    Queue<PaintAction> queue=new LinkedList<PaintAction>();
    public ServerModel(int width, int height){
        pModel=new PaintingModel("server", width, height);
        count=1;

    }

    public ServerInterface getServerInterface() {
        return server;
    }

    public void setServerInterface(ServerInterface server) {
        this.server = server;
    }

    private ServerInterface server;

    public void onAction(PaintAction action)
    {
        action.setID(++count);
        queue.add(action);
        if (queue.size()>30)
            queue.poll();
        pModel.addAction(action);
        pModel.push();
        if (server!=null)
            server.applyAction(action);
    }

    public int getCount() {
        return count;
    }

    public List<PaintAction> getActions(int number){
        if (queue.isEmpty()) return new ArrayList<PaintAction>();
        if (queue.peek().getID()>number) return null;
        ArrayList<PaintAction> list=new ArrayList<PaintAction>();
        for(PaintAction pa:queue)
          if (pa.getID()>number) list.add(pa);
        return list;
    }

    public BufferedImage getImage()
    {
        return pModel.getImage();
    }
}
